package com.itheima.tlias84.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.tlias84.annotation.Log;
import com.itheima.tlias84.dto.SelectEmpDTO;
import com.itheima.tlias84.dto.SelectEmpDetailDTO;
import com.itheima.tlias84.entity.Emp;
import com.itheima.tlias84.entity.Expr;
import com.itheima.tlias84.mapper.EmpMapper;
import com.itheima.tlias84.mapper.ExprMapper;
import com.itheima.tlias84.po.InsertDeptParam;
import com.itheima.tlias84.po.InsertEmpParam;
import com.itheima.tlias84.po.SelectEmpPageParam;
import com.itheima.tlias84.po.UpdateEmpParam;
import com.itheima.tlias84.service.EmpService;
import com.itheima.tlias84.utils.ResultUtil;
import com.itheima.tlias84.vo.PageVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Resource
    EmpMapper empMapper;

    @Resource
    ExprMapper exprMapper;

    @Override
    public ResultUtil selectEmpByPage(SelectEmpPageParam param) {
        //手动计算分页
        //1    5   ->  0 ,5
        //2    5   ->  (page-1)* pageSize, pageSize
        //3    5   ->  2 * 5,5
        //4    5   ->  3 * 5,5
        // param.setPage((param.getPage()-1)* param.getPageSize());
        //注意 pageHelper 会自动执行一个count sql ,并且会自动拼接limit
        //因此必须写在自己的sql之前
        Page<Object> page = PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SelectEmpDTO> dtos = empMapper.selectEmpByPage(param);
        PageVO<SelectEmpDTO> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(dtos);
        return ResultUtil.success(pageVO);
    }

    @Override
    public List<Emp> selectEmp() {
        return empMapper.selectEmp();
    }

    @Log
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int batchDelete(List<Integer> ids) {
        //先删除员工
        int i = empMapper.batchDelete(ids);
        //再删除员工履历
        int i1 = exprMapper.batchDeleteExpr(ids);
        log.info("删除{}条履历",i1);
        return i;
    }


    @Log
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertEmp(InsertEmpParam param) {
        //设置时间
        param.setCreateTime(LocalDateTime.now());
        param.setUpdateTime(LocalDateTime.now());
        int flag = empMapper.insertEmp(param);
        //插入工作履历
        log.info("员工回显的主键{}", param.getId());
        List<Expr> exprList = param.getExprList();
        if (CollectionUtils.isNotEmpty(exprList)) {
            //赋值员工id 以及时间
            exprList = exprList.stream().map(t -> {
                t.setEmpId(param.getId());
                t.setCreateTime(LocalDateTime.now());
                t.setUpdateTime(LocalDateTime.now());
                return t;
            }).toList();
            int i = exprMapper.batchInsertExpr(exprList);
            log.info("插入员工履历{}", i);
        }
        return flag;
    }

    @Override
    public SelectEmpDetailDTO selectEmpById(Integer id) {
        //先查询员工
        SelectEmpDetailDTO dto = empMapper.selectEmpById(id);
        //再查询履历
        List<Expr> exprs = exprMapper.selectExprByEmpId(id);
        dto.setExprList(exprs);
        return dto;
    }

    @Log
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateEmp(UpdateEmpParam param) {
        param.setUpdateTime(LocalDateTime.now());
        //1.先更新员工表信息
        int empRows = empMapper.updateEmp(param);
        //2.再删除该员工的所有履历
        int exprDelRows = exprMapper.batchDeleteExpr(List.of(param.getId()));
        log.info("删除{}条旧履历数据",exprDelRows);
        //3.新增所有履历
        List<Expr> exprList = param.getExprList();
        if (CollectionUtils.isNotEmpty(exprList)) {
            //赋值员工id 以及时间
            exprList = exprList.stream().map(t -> {
                t.setEmpId(param.getId());
                t.setCreateTime(LocalDateTime.now());
                t.setUpdateTime(LocalDateTime.now());
                return t;
            }).toList();
            int i = exprMapper.batchInsertExpr(exprList);
            log.info("插入{}条新员工履历", i);
        }
        return empRows;
    }
}
